/**
 * com.chaeron.GPS - An API for access to GPS devices.
*
*
* Copyright (c) 2001, 2002 Chaeron Corporation,
* All rights reserved.
* 
* Chaeron grants you a non-exclusive license to use, modify and re-distribute
* this program under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version, provided that this copyright notice and 
* license appear on all copies of the software.
* 
* Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
* IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
* HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
* BY THE LICENSEE. See the GNU General Public License for more details.
*
* CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
* IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
* PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
* OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

* @version     1.02
* @author      Andrzej Jan Taramina, Chaeron Corporation
*              andrzej@chaeron.com
*/

package com.chaeron.GPS.exception;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import java.util.*;

//***************************************************************************
//*
//*     GPSException class definition
//*
//***************************************************************************/

/**
 * The GPSException class is the base class for all GPS exceptions
*
*/

public abstract class GPSException extends Exception
{
	
	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	/**
	 * The constant for an unused Code
	*/
	public final static int 			EXCEPTION_CODE_UNUSED 			= -1;
	
	/**
	 * The constant for an Unknown Code
	*/
	public final static int 			EXCEPTION_CODE_UNKNOWN 			= 0;
	
	/**
	 * The constant string for an Invalid Code
	*/
	protected final static String 		EXCEPTION_CODE_INVALID_MESSAGE 	= "Invalid";
	
	/**
	 * The constant string for an Unknown Code
	*/
	protected final static String 		EXCEPTION_CODE_UNKNOWN_MESSAGE 	= "Unknown";
	
	
	//***************************************************************************
	//*
	//*     Attribute definitions
	//*
	//***************************************************************************/
	
	protected int			code				= EXCEPTION_CODE_UNUSED;
	
	protected String[]   	exceptionMessages 	= {};
	
	
	//***************************************************************************
	//*
	//*     Constructors
	//*
	//***************************************************************************/
	
	
	/**
	 * Constructs a <code>GPSException</code> with <code>null</code>
	* as its error detail message.
	*/
	
	public GPSException() 
	{
		super();
	}
	
	
	/**
	 * Constructs a <code>GPSException</code> with the specified detail
	* message. The error message string <code>s</code> can later be
	* retrieved by the <code>getMessage()</code>
	* method.
	*
	* @param   s   the detail message.
	*/
	
	public GPSException( String s ) 
	{
		super( s );
	}
	
	
	/**
	 * Constructs a <code>GPSException</code> with the specified detail
	* message and GPS exception code. 
	*
	* @param   s   the detail message.
	* @param   code   the GPS Exception code
	*/
	
	public GPSException( String s, int code ) 
	{
		super( s );
		
		this.code = code;
	}
	
	//***************************************************************************
	//*
	//*     Method Signatures
	//*
	//***************************************************************************/
	
	/**
	 * Return the message for this exception
	*
	* @return   the message
	*/
	
	public String getMessage() 
	{
		String		codeMsg = getCodeMessage();
		String		msg 	= super.getMessage();
		
		if( codeMsg != null ) {
			msg += ": " + getCodeMessage();
		}
		
		return( msg );
	}
	
	
	/**
	 * Return the specific GPS Exception code 
	*
	* @return   the GPS Exception code
	*/
	
	public int getCode() 
	{
		return( code );
	}
	
	
	/**
	 * Return the specific GPS Exception code message
	* Default is null.  
	*
	* @return   the GPS Exception code message
	*/
	
	public String getCodeMessage() 
	{
		String	msg	= null;
		
		switch( code ) {
			case EXCEPTION_CODE_UNUSED:
				msg	= null;
				break;
				
			case EXCEPTION_CODE_UNKNOWN:
				msg	= EXCEPTION_CODE_UNKNOWN_MESSAGE;
				break;	
				
			default:
				msg	= getSubclassCodeMessage();
				break;
		}
		
		return( msg );
	}

	/**
	 * Return the specific GPS Exception code message
	 * for this subclass of GPSException
	 *
	 * @return   the GPS Exception code message for this subclass
	 */
	
	private  String getSubclassCodeMessage()
	{
		String		msg = null;
		
		if( code <= 0 || code >= exceptionMessages.length ) {
			msg = EXCEPTION_CODE_INVALID_MESSAGE;
		} else {
			msg = exceptionMessages[ code ];
		}
		
		return( msg );
	}
	
}
